class Node {
    constructor(val) {
        this.val = val 
        this.next = null
    }
}
class linkedList {
    constructor() {
        this.count = 0 
        this.head = null 
    }
    indexOf(val){
        // return index
        let current = this.head 
        let index = 0
        while (current.next) {
            if (current.val === val) {
                return index
            } else {
                current = current.next 
                index++ 
            }
        }
        return current.val === val ? index : -1
    }
    // 向尾部添加一个新元素
    push(element) {
        const node = new Node(element)
        if (this.head === null)  {
            this.head = node 
        } else {
            let current = this.head
            while (current.next) {
                current = current.next 
                
            }
            current.next = node 
        }
        this.count++
    }
    insert(element, position) {
        // 先判断位置 
        if (position > this.count) return false
        const newNode = new Node(element)
        let current = this.head
        this.count++
        if (position === 0) {
            this.head = newNode 
            newNode.next  =  current
            return 
        }
        while (position > 1) {
            current = current.next 
            position-- 
        }
        newNode.next = current.next 
        current.next = newNode
    }
    getElementAt(index) {
        if (index >=0 && index < this.count ) {
            let current = this.head 
            while(index) {
                current = current.next 
                index--
            }
            return current.val
        }
        return false
    }
    removeAt(index) {
        if (index >=0 && index < this.count) {
            let current = this.head
            if (index === 0) {
                this.head = current.next 
            } else {
                while (index > 1) {
                    current = current.next 
                    index--
                }
                current.next = current.next.next
            }
            this.count--
            return true
        }
        return false
    }
    size() {
        return this.count 
    }
    isEmpty() {
        return this.count === 0 ? true : false
    }
    toString() {
        let count = this.count
        if (!count) return ""
        let str = this.head.val 
        let current = this.head
        
        while(count > 1) {
            current = current.next
            str += "->" + current.val
            count--
        }
        return str
    }
}   
function arrayToLinkedList(array) {
    const list = new linkedList()
    if (!(array instanceof Array)) return new Node()
    array.forEach(el => {
        list.push(el)
    }); 
    return list.head
}
class listNode {
    constructor(val, next) {
        this.val = val 
        this.next = next ? next : null
    }
}

// const list = new linkedList()
// // 2.追加元素
// list.append(15)
// list.append(10)
// list.append(20)
// console.log(list, "append");
// list.insert(0, 100)
// list.insert(4, 200)
// list.insert(2, 300)
// console.log(list, "insert");
// console.log(list.removeAt(0), 'rA');
// console.log(list.removeAt(1), 'rA');
// console.log(list.removeAt(3), 'rA');
// console.log(list);
// console.log(list.indexOf(15))
// console.log(list.indexOf(10)) 
// console.log(list.indexOf(20))
// console.log(list.indexOf(100))
export {
    Node,
    linkedList,
    arrayToLinkedList,
    listNode
}